package com.itheima.springbootwebtlias.controller;

import com.itheima.springbootwebtlias.pojo.ClaQueryParam;
import com.itheima.springbootwebtlias.pojo.Clazz;
import com.itheima.springbootwebtlias.pojo.PageResult;
import com.itheima.springbootwebtlias.pojo.Result;
import com.itheima.springbootwebtlias.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    //查询所有班级
    @GetMapping("/list")
    public Result QueryALLClazz() {
        log.info("查询全部班级列表，请求参数：无");
        List<Clazz> clazzs = clazzService.selectAll();
        log.info("查询全部班级列表，查询完毕：{}", clazzs);
        return Result.success(clazzs);
    }

    //新增班级
    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("新增班级，请求参数：{}", clazz);
        clazzService.saveClazz(clazz);
        //log.info("新增班级，结果：{}", clazz);
        return Result.success();
    }

    //班级列表分页查询
    @GetMapping
    public Result page(ClaQueryParam queryParam){
        log.info("分页条件查询，请求参数：{}", queryParam);
        PageResult pageResult = clazzService.page(queryParam);
        return Result.success(pageResult);
    }

    //删除班级
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Integer id) {
        log.info("删除班级，请求参数：{}", id);
        clazzService.removeById(id);
        return Result.success();
    }

    //根据id查询班级
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id) {
        log.info("根据id查询班级，请求参数：{}", id);
        Clazz clazz = clazzService.queryById(id);
        return Result.success(clazz);
    }

    //修改班级
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级，请求参数：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }
}
